﻿#region File Description
//-----------------------------------------------------------------------------
// Game1.cs
//
// XNAHero.com
// Copyright (C) XNAHero.com. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using _3dtextxna.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace rendertexture3dxna
{
    public struct TexturedQuad
    {
        public readonly RectangleF SourceRectangleUv;

        public readonly Vector3 Origin;
        public readonly Vector3 Normal;
        public readonly Vector3 Up;
        public readonly Vector3 Left;

        public readonly Vector2 Size;


        public readonly Vector3 UpperLeft;
        public readonly Vector3 LowerLeft;
        public readonly Vector3 UpperRight;
        public readonly Vector3 LowerRight;

        public VertexPositionNormalTexture[] Vertices;
        public short[] Indexes;

        public TexturedQuad(Vector3 origin, Vector3 normal, Vector3 up, Rectangle sourceRectangle, Vector2 textureSize, Vector2 size)
        {
            Origin = origin;
            Normal = normal;
            Up = up;
            Size = size;

            SourceRectangleUv = new RectangleF(
                sourceRectangle.Left / (textureSize.X),
                sourceRectangle.Top / (textureSize.Y),
                sourceRectangle.Width / (textureSize.X),
                sourceRectangle.Height / (textureSize.Y)
                );


            // Calculate the quad corners
            Left = Vector3.Cross(normal, Up);
            Vector3 uppercenter = (Up * Size.Y / 2) + origin;
            UpperLeft = uppercenter + (Left * Size.X / 2);
            UpperRight = uppercenter - (Left * Size.X / 2);
            LowerLeft = UpperLeft - (Up * Size.Y);
            LowerRight = UpperRight - (Up * Size.Y);

            Vertices = new VertexPositionNormalTexture[4];
            Indexes = new short[6];

            InitializeVertices();

        }
        public TexturedQuad(Vector3 origin, Vector3 normal, Vector3 up, RectangleF sourceRectangleUv, Vector2 size)
        {
            Origin = origin;
            Normal = normal;
            Up = up;
            Size = size;

            SourceRectangleUv = sourceRectangleUv;

            // Calculate the quad corners
            Left = Vector3.Cross(normal, Up);
            Vector3 uppercenter = (Up * Size.Y / 2) + origin;
            UpperLeft = uppercenter + (Left * Size.X / 2);
            UpperRight = uppercenter - (Left * Size.X / 2);
            LowerLeft = UpperLeft - (Up * Size.Y);
            LowerRight = UpperRight - (Up * Size.Y);

            Vertices = new VertexPositionNormalTexture[4];
            Indexes = new short[6];

            InitializeVertices();
        }

        public void InitializeVertices()
        {
            // Provide a normal for each vertex
            for (int i = 0; i < Vertices.Length; i++)
            {
                Vertices[i].Normal = Normal;
            }

            // Set the position and texture coordinate for each
            // vertex
            Vertices[0].Position = LowerLeft;
            Vertices[0].TextureCoordinate = new Vector2(SourceRectangleUv.Left, SourceRectangleUv.Bottom);

            Vertices[1].Position = UpperLeft;
            Vertices[1].TextureCoordinate = new Vector2(SourceRectangleUv.Left, SourceRectangleUv.Top);

            Vertices[2].Position = LowerRight;
            Vertices[2].TextureCoordinate = new Vector2(SourceRectangleUv.Right, SourceRectangleUv.Bottom);

            Vertices[3].Position = UpperRight;
            Vertices[3].TextureCoordinate = new Vector2(SourceRectangleUv.Right, SourceRectangleUv.Top);

            // Set the index buffer for each vertex, using
            // clockwise winding
            Indexes[0] = 0;
            Indexes[1] = 1;
            Indexes[2] = 2;
            Indexes[3] = 2;
            Indexes[4] = 1;
            Indexes[5] = 3;
        }
    }
}
